<?php
class KomaSystemAdmin extends KomaComponentAdmin
{
    function main()
    {
        switch($this->getParam('action', 'g'))
        {
            case 'addblockform':
                $form = new KomaContainerForm(null, null, $this->URL.'/?action=addblock');
                $form->add(new KomaWidgetText('blockTitle', null, T_('Block Title')));
                $form->add(new KomaWidgetTextArea('blockContent', null, 'Block Content'));
                $form->add('<br /><br />');
                $form->add(new KomaWidgetDialog('preview', 'Block Preview', ''));
                $form->add(new KomaWidgetCheckbox('ignoretitle', 'Ignore title, check this if you want to save or preview block without block title.'));
                $form->add('<br /><br />');
                $form->add(new KomaWidgetButton('previewlink', 'Preview'));
                $form->add(new KomaWidgetButton('save', 'Save'));
                
                
                KomaScriptManager::getInstance()->addScript('$(\'#previewlink\').click(function() { var preview = \'\'; if(!$(\'#ignoretitle\').is(\':checked\')) { preview += \'<table class="block_title"><tr><td>\' + $(\'#blockTitle\').val() + \'<td></tr></table>\'; } preview += \'<table class="blocks"><tr><td>\' + $(\'#blockContent\').val() + \'<td></tr></table>\'; $(\'#preview\').html(preview).dialog(\'open\'); return false; });');
                
                $this->Template->File = 'addblock.html';
                $this->Template->assign('addBlockForm', $form);
                break;
            case 'addblock':
                echo 'Ajax Test Again';
                $this->Render = false;
                break;
            case 'configform':
                $this->Database->query("SELECT * FROM koma_config");
                
                $form = new KomaContainerForm(null, null, $this->URL.'?action=saveconf');
                $layout = new KomaLayoutForm();
                
                while($row = $this->Database->fetchAssoc())
                {
                    $layout->add(new KomaWidgetText($row['config_name'], $row['config_value'], $row['config_value'], array('style' => 'width:90%;')) . '<br /><sup>'.$row['comment'].'</sup>', $row['config_name'].'&nbsp;');
                }
                
                $layout->add(new KomaWidgetButton('save', 'Save', KOMA_BUTTON_SUBMIT));
                $form->add($layout);
                
                echo $form;
                $this->Render = false;
                
                break;
            case 'configsave':
                $result = $this->Database->query("SELECT * FROM koma_config");
                $modified = false;
                
                while($row = $this->Database->fetchAssoc($result))
                {
                    if($this->getParam($row['config_name']) != $row['config_value'])
                    {
                        $modified = true;
                        $newvalue = null;
                        
                        if(is_numeric($row['config_value']) && !$this->getParam($row['config_name'])) $newvalue = '0';
                        else $newvalue = $this->getParam($row['config_name']);
                        
                        $this->Database->query(sprintf("UPDATE `%s` SET `%s` = '%s' WHERE `%s` = '%s'", 'koma_config', 'config_value', $newvalue, 'config_name', $row['config_name']));
                    }
                }
                
                if($modified) { redirect($this->URL, 'New Configuration Has Been Saved'); }
                redirect($this->URL);
                break;
            default:
                $this->Database->query("SELECT * FROM koma_config");
                
                $form = new KomaContainerForm('config', $this->URL.'?action=configsave');
                $layout = new KomaLayoutForm();
                
                while($row = $this->Database->fetchAssoc())
                {
                    if($row['config_name'] == 'Locale') $layout->add(new KomaWidgetListBox($row['config_name'], KomaLists::getInstalledLocales(), $row['config_value']) . '<br /><sup>'.$row['comment'].'</sup>', $row['config_name'].'&nbsp;');
                    else if($row['config_name'] == 'Theme') $layout->add(new KomaWidgetListBox($row['config_name'], KomaLists::getInstalledThemes(), $row['config_value']) . '<br /><sup>'.$row['comment'].'</sup>', $row['config_name'].'&nbsp;');
                    else if($row['config_name'] == 'IsOffline' || $row['config_name'] == 'Debug') $layout->add(new KomaWidgetListBox($row['config_name'], array('0' => 'false', '1' => 'true'), $row['config_value']) . '<br /><sup>'.$row['comment'].'</sup>', $row['config_name'].'&nbsp;');
                    else $layout->add(new KomaWidgetText($row['config_name'], null, $row['config_value'], array('style' => 'width:90%;')) . '<br /><sup>'.$row['comment'].'</sup>', $row['config_name'].'&nbsp;');
                }
                
                $layout->add(new KomaWidgetButton('save', 'Save', KOMA_BUTTON_SUBMIT));
                $form->add($layout);
                
                echo new KomaLayoutTabPanel('maintabs', array('Website Configuration' => $form,
                                                          'Block Configuration' => 'Blocks',
                                                          'Ajax' => new KomaWidgetHyperlink(null, $this->URL.'?action=addblock', 'Ajax')));
        }
    }
}
?>
